require('dotenv').config();
const mongoose = require('mongoose');
const Device = require('../models/Device');

const sampleDevices = [
  {
    name: '大门监控摄像头',
    type: 'camera',
    location: '主入口',
    status: 'online',
    ip: '192.168.1.101',
    port: 80,
    specifications: {
      model: 'DS-2CD2T47G1-L',
      manufacturer: '海康威视',
      firmware: 'V5.6.3',
      installDate: new Date('2023-01-15')
    }
  },
  {
    name: '温湿度传感器01',
    type: 'sensor',
    location: 'A栋1楼大厅',
    status: 'online',
    ip: '192.168.1.102',
    port: 80,
    specifications: {
      model: 'SHT30',
      manufacturer: '博世',
      firmware: 'V2.1.0',
      installDate: new Date('2023-02-01')
    }
  },
  {
    name: '门禁控制器',
    type: 'access_control',
    location: 'A栋主入口',
    status: 'online',
    ip: '192.168.1.103',
    port: 80,
    specifications: {
      model: 'AC-2000',
      manufacturer: '中控智慧',
      firmware: 'V3.2.1',
      installDate: new Date('2023-01-20')
    }
  },
  {
    name: '烟雾报警器',
    type: 'alarm',
    location: 'B栋2楼',
    status: 'offline',
    ip: '192.168.1.104',
    port: 80,
    specifications: {
      model: 'JTY-GD-SA1201',
      manufacturer: '海湾安全',
      firmware: 'V1.5.2',
      installDate: new Date('2023-03-01')
    }
  },
  {
    name: 'LED照明控制器',
    type: 'lighting',
    location: '停车场',
    status: 'online',
    ip: '192.168.1.105',
    port: 80,
    specifications: {
      model: 'LC-100',
      manufacturer: '飞利浦',
      firmware: 'V2.0.1',
      installDate: new Date('2023-02-15')
    }
  },
  {
    name: '空调控制器',
    type: 'hvac',
    location: 'A栋办公区',
    status: 'maintenance',
    ip: '192.168.1.106',
    port: 80,
    specifications: {
      model: 'HVAC-200',
      manufacturer: '格力',
      firmware: 'V1.8.0',
      installDate: new Date('2023-01-10')
    }
  }
];

async function seedDevices() {
  try {
    // 连接数据库
    await mongoose.connect('mongodb+srv://gzhukun:gzk135246@cluster0.pnlj5a1.mongodb.net/smartpark');
    
    console.log('数据库连接成功');
    
    // 清空现有设备数据
    await Device.deleteMany({});
    console.log('清空现有设备数据');
    
    // 插入示例设备
    await Device.insertMany(sampleDevices);
    console.log('插入示例设备成功');
    
    console.log(`成功创建 ${sampleDevices.length} 个设备`);
    
    process.exit(0);
  } catch (error) {
    console.error('创建设备数据失败:', error);
    process.exit(1);
  }
}

seedDevices();